/*
 * @Author: crissu 13750782397@163.com
 * @Date: 2024-05-29 01:58:53
 * @LastEditors: crissu 13750782397@163.com
 * @LastEditTime: 2024-05-29 02:00:29
 * @FilePath: /try_concurrence/MemoryAccessSynchronization/tryOnce.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package memoryaccesssynchronization

import (
	"fmt"
	"sync"
)

// Ensuring that only one execution will be carried out even among several goroutines
func TryOnce() {
	var count int

	increment := func() {
		count++
	}

	var once sync.Once

	var increments sync.WaitGroup
	increments.Add(100)

	for i := 0; i < 100; i++ {
		go func() {
			defer increments.Done()
			once.Do(increment)
		}()
	}

	increments.Wait()
	fmt.Printf("Count is %d\n", count)
}
